﻿using System.Collections.Generic;
using System.Linq;
using Softelligent.Zintra.Synchronizations.ContactsExpressConsole.Entities;

namespace Softelligent.Zintra.Synchronizations.ContactsExpressConsole
{
    public class ContactMapper
    {
        public ClientSDK.Contacts.Contact Map(Contact entity)
        {
            if (entity == null)
            {
                return null;
            }
            var dto = new ClientSDK.Contacts.Contact
            {
                Id = entity.Id.ToString(),
                UnformattedName = entity.FullName(),
                FirstName = entity.FirstName,
                LastName = entity.LastName,
                WorkEMailAddress = entity.WorkEmailAddress,
                HomeEMailAddress = entity.HomeEmailAddress
            };
            if (entity.Address != null)
            {
                var address = entity.Address;
                dto.WorkPostalAddress = new ClientSDK.Contacts.PostalAddress
                                            {
                                                Street = address.Street,
                                                City = address.City,
                                                PostalCode = address.PostalCode,
                                                State = address.State,
                                                Country = address.Country
                                            };
            }

            return dto;
        }

        public Contact Map(ClientSDK.Contacts.Contact dto)
        {
            return Map(dto, new Contact());
        }

        public Contact Map(ClientSDK.Contacts.Contact dto, Contact entity)
        {
            if (dto == null || entity == null)
            {
                return null;
            }
            var id = 0;
            if (int.TryParse(dto.Id, out id))
            {
                entity.Id = id;
            }
            entity.UnformattedName = dto.UnformattedName;
            entity.FirstName = dto.FirstName;
            entity.LastName = dto.LastName;
            entity.WorkEmailAddress = dto.WorkEMailAddress;
            entity.HomeEmailAddress = dto.HomeEMailAddress;
            var address = dto.WorkPostalAddress;
            if (entity.Address == null)
            {
                entity.Address = new Address
                                      {
                                          Street = address.Street,
                                          City = address.City,
                                          PostalCode = address.PostalCode,
                                          State = address.State,
                                          Country = address.Country
                                      };
            }
            else if (address == null)
            {
                entity.Address = null;
            }
            else
            {
                entity.Address.Street = address.Street;
                entity.Address.City = address.City;
                entity.Address.PostalCode = address.PostalCode;
                entity.Address.State = address.State;
                entity.Address.Country = address.Country;
            }

            return entity;
        }

        public List<ClientSDK.Contacts.Contact> Map(IEnumerable<Contact> entitys)
        {
            if (entitys == null || !entitys.Any())
            {
                return new List<ClientSDK.Contacts.Contact>();
            }

            var contacts = new List<ClientSDK.Contacts.Contact>();
            foreach (var entity in entitys)
            {
                contacts.Add(Map(entity));
            }

            return contacts;
        }
    }
}
